<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class UserGiftBagController extends BaseController
{

    /**
     * @api {post} /user-manager/user-gift-bag 用户礼包创建
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户礼包创建
     * @apiGroup 会员管理
     * @apiParam {number} name                              礼包名称
     * @apiParam {number} status                            状态 1开启0关闭
     * @apiParam {json}   gift_bag_detail                   礼包内容 [{"foreign_id":1,"name":"名称","type":1,"quantity":10,"days":2,"gold":500}]
     * @apiParam {number} foreign_id     挂件id/坐骑id/礼物id
     * @apiParam {number} name           挂件名称/坐骑名称/礼物名称
     * @apiParam {number} type           礼包类型 1金币2礼物3坐骑4头像框
     * @apiParam {number} quantity       礼包内容数量
     * @apiParam {number} days           有效天数
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name'            => 'required|string|max:255',
                'status'          => 'required|numeric|integer|in:0,1',
                'gift_bag_detail' => 'required|string',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }

        $params = $request->input();

        $res = app('repo_user');

        $rows = $res->addUserGiftBag($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /user-manager/user-gift-bag 用户礼包删除
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户礼包删除
     * @apiGroup 会员管理
     * @apiParam {string} ids                       字符串分割的id 必传
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function del(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode(",", $idsStr);

        $rows = app('repo_user')->delUserGiftBag($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /user-manager/user-gift-bag 用户礼包修改
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户礼包修改
     * @apiGroup 会员管理
     * @apiParam {number} id
     * @apiParam {number} name                              礼包名称
     * @apiParam {number} status                            状态 1开启0关闭
     * @apiParam {json}   gift_bag_detail                   礼包内容 [{"foreign_id":1,"name":"名称","type":1,"quantity":10,"days":2,"gold":500}]
     * @apiParam {number} foreign_id     挂件id/坐骑id/礼物id
     * @apiParam {number} name           挂件名称/坐骑名称/礼物名称
     * @apiParam {number} type           礼包类型 1金币2礼物3坐骑4头像框
     * @apiParam {number} quantity       礼包内容数量
     * @apiParam {number} days           有效天数
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function update(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'            => 'required|integer',
                'name'            => 'required|string|max:255',
                'status'          => 'required|numeric|integer|in:0,1',
                'gift_bag_detail' => 'required|string',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, getValidateMessage($v->messages()));
        }

        $res = app('repo_user');

        $params = $request->input();
        $id = $params['id'];

        $rows = $res->saveUserGiftBag($id, $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /user-manager/user-gift-bag/send 赠送礼包
     * @apiVersion 0.1.0
     * @apiName 会员管理-赠送礼包
     * @apiGroup 会员管理
     * @apiParam {int} id
     * @apiParam {string} user_codes                        逗号分割的用户编号字符串
     * @apiSuccessExample 成功响应
     *      {"code":200,"data":true}
     * @apiError 10001 参数校验失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function send(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'user_codes' => 'required|string',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $res = app('repo_user');
        $params = $request->input();
        $id = $params['id'];
        $userCodeArr = explode(",", $params['user_codes']);
        $userList = app("model_user_data")->whereIn("user_code", $userCodeArr)->where([["user_code", "<>", 0]])->select(["user_id"])->get();
        $userIdList = empty($userList) ? [] : array_column($userList->toArray(), "user_id");

        if (count($userCodeArr) != count($userIdList)) {
            throw new BasicException(10001, '存在不合法的用户编号！');
        }
        $idArr = $userIdList;

        $admin_id = $request->admin_id;
        $rows = $res->sendUserGiftBag($id, $idArr,$admin_id);
        if (!$rows) {
            throw new BasicException(10001, "发送失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /user-manager/user-gift-bag 用户礼包列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户礼包列表
     * @apiGroup 会员管理
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(序号)": 32,
     * "name(礼包名称)": "老玩家回归礼包12",
     * "add_time(添加时间)": "2020-09-09",
     * "status(状态 1开启0关闭)": 1,
     * "gift_bag_detail(礼包内容)": [
     * {
     * "foreign_id(挂件id/坐骑id/礼物id)": 321,
     * "name(挂件名称/坐骑名称/礼物名称)": "爆音卡",
     * "type(礼包类型 1金币2礼物3坐骑4头像框)": 2,
     * "quantity(礼包内容数量)": 2,
     * "gold(金币数量)": 0,
     * "days(有效天数)": 0
     * }
     * ]"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 10,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function list(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page'    => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_user')->getUserGiftBagQueryList($request->input());

        return Response::sendList($list);
    }

    /**
     * @api {get} /user-manager/user-gift-bag/select/list 用户礼包下拉框
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户礼包下拉框
     * @apiGroup 会员管理
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "ride": [
     * {
     * "id(坐骑id)": 20,
     * "title(坐骑名称)": "黄金幻影"
     * }
     * ],
     * "head": [
     * {
     * "id(挂件id)": 20,
     * "title(挂件名称)": "黄金幻影"
     * }
     * ],
     * "gift": [
     * {
     * "id(礼物id)": 20,
     * "name(礼物名称)": "黄金幻影"
     * }
     * ]
     * }
     * }
     */
    public function selectList()
    {
        $list = app('repo_user')->getUserGiftBagSelectList();

        return Response::sendData($list);
    }


    /**
     * @api {get} /user-manager/receive-gift-bag 接收礼包用户-列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-接收礼包用户-列表
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} gift_bag_id 礼包id 可选
     * @apiParam {string} nickname 用户昵称 可选
     * @apiParam {string} invite_nickname 邀请人昵称 可选
     * @apiParam {number} invite_user_code 邀请人用户编号 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "parent_id(")": 0,
     * "gift_bag_id(礼包id)": 33,
     * "add_time(接收时间)": "2020-11-09 20:36:15",
     * "nickname(昵称)": "气泡用户748438",
     * "user_code(用户编号)": 748438,
     * "gift_bag_name(礼包名称)": "老玩家回归礼包13",
     * "invite_nickname(邀请人昵称)": "气泡用户386910",
     * "invite_user_code(邀请人用户编号)": 386910,
     * "gift_bag_detail(礼包内容)": [
     * {
     * "foreign_id(挂件id/坐骑id/礼物id)": 321,
     * "name(挂件名称/坐骑名称/礼物名称)": "爆音卡",
     * "type(礼包类型 1金币2礼物3坐骑4头像框)": 2,
     * "quantity(礼包内容数量)": 2,
     * "gold(金币数量)": 0,
     * "days(有效天数)": 0
     * }
     * ]
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 10,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function receiveList(Request $request)
    {
        //参数校验
        //        try {
        $validator = Validator::make($request->all(), [
            'gift_bag_id' => 'filled|numeric',
            'perPage'     => 'filled|numeric',
            'page'        => 'filled|numeric',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }
        //        } catch (\Exception $e) {
        //            throw new BasicException(10001, json_encode($e->errors()));
        //        }

        $list = app('repo_user')->getReceiveGiftBagQueryList($request->input());


        return Response::sendList($list);
    }

}
